package com.victor.controller;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.victor.utils.FileUpload;
import com.victor.utils.Msg;
import com.victor.utils.OSSUtil;

@RestController
@RequestMapping("file")
public class FileController {
	private Logger logger = LoggerFactory.getLogger(FileController.class);
	@Autowired
	private HttpServletRequest request;

	@PostMapping("/upload")
	public Msg upload(MultipartFile file) {
		String uploadFile = null;
		try {
			uploadFile = FileUpload.uploadFile(file, request, "file");
		} catch (Exception e) {
			logger.error("--FileController.errorInfo>:{}", e);
			return Msg.fail();
		}

		return Msg.success().add("path", uploadFile);
	}
	
	@PostMapping("/uploadphoto")
	public Msg uploadphoto(MultipartFile file) {
		if (file==null || file.isEmpty()) {
			return Msg.fail();
		}
		String uploadImg2Oss = OSSUtil.uploadImg2Oss(file);
		return Msg.success().add("photopath", uploadImg2Oss);
		
	}

}
